﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;

public partial class Combat_BrigadeList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);	
        if (!IsPostBack)
        {
            BindNoneReceive();
            ListPageContral1.SetFirstPage();
            BindReceive(0);
        }
    }

    //任务标题
    public string GetTaskTitle(object taskID)
    {
        if (taskID == null)
            return "";
        TaskBaseInfo taskInfo= TaskBase.GetTaskBase(new Guid(taskID.ToString()));
        return taskInfo != null ? taskInfo.TaskTitle : "";
    }
    //任务时间
    public string GetTaskDate(object taskID)
    {
        if (taskID == null)
            return "";
        TaskBaseInfo taskInfo = TaskBase.GetTaskBase(new Guid(taskID.ToString()));
        return taskInfo != null ? taskInfo.SetOnDate.ToString("yyyy-MM-dd") : "";
    }
    public string GetTaskID(object taskID)
    {
        return taskID.ToString();
    }
    public string GetText(object id)
    {
        if (id == null)
            return "";
        int count= TaskSquadron.GetCount("TaskBrigadeID ='"+id.ToString()+"'");
        return count <= 0 ? "未下达" : "已下达["+count.ToString()+"]";
    }
    private void BindNoneReceive()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        UserGrid.DataSource = TaskBrigade.GetList(" IsAccept='0' And Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "' And TaskID IN (Select TaskID From TaskBase Where EndDate>'" + DateTime.Now + "' And IsOn='1' )");
        UserGrid.DataBind();
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindReceive(PageIndex);
    }
    private void BindReceive(int pageIndex)
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;

        int count = TaskBrigade.GetCount(" IsAccept='1' And Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "'");
        ListPageContral1.ItemCount = count;
        TaskList.DataSource = TaskBrigade.GetList("AcceptDate", "Desc", " (IsAccept='1' Or (IsAccept='0' And TaskID IN (Select TaskID From TaskBase Where EndDate<'"+DateTime.Now+"' And IsOn='1')) ) And Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "'", ListPageContral1.PageSize, pageIndex);
        TaskList.DataBind();
    }
    public void lnbtnReceive_Command(object sender, CommandEventArgs e)
    {
        TaskBrigadeInfo info = TaskBrigade.GetTaskBrigade(new Guid(e.CommandArgument.ToString()));
        if (info == null)
            return;       
        info.IsAccept = true;
        info.AcceptDate = DateTime.Now;
        info.AcceptUser = User.Identity.Name;
        TaskBrigade.Update(info);

        BindNoneReceive();
        ListPageContral1.SetFirstPage();
        BindReceive(0);        
    } 

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void TaskList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}
